﻿#include "formchannelcoefficient.h"
#include "ui_formchannelcoefficient.h"
#include "globalsymbol.h"

#include <QDebug>
#include <QMessageBox>

FormChannelCoefficient::FormChannelCoefficient(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormChannelCoefficient)
{
    ui->setupUi(this);

    // 初始化
    initUI();
    init();
}

FormChannelCoefficient::~FormChannelCoefficient()
{
    delete ui;
}

void FormChannelCoefficient::setChannelCoefficient(const QList<double> &coefficientList)
{
    for (int i = 0; i < coefficientList.size(); i++) {
        QString id = QString("%1").arg(i, 3, 10, QLatin1Char('0'));
        QDoubleSpinBox* dsb = ui->gbxChannelCoefficient->findChild<QDoubleSpinBox*>(QString("dsbD%1").arg(id));
        dsb->setValue(coefficientList.at(i));
    }
}

void FormChannelCoefficient::initUI()
{
    // 设置修改的可用性
    for (int i = 0; i < CHANNEL_COUNT; i++) {
        QString id = QString("%1").arg(i, 3, 10, QLatin1Char('0'));
        QDoubleSpinBox* dsb = ui->gbxChannelCoefficient->findChild<QDoubleSpinBox*>(QString("dsbD%1").arg(id));
        dsb->setEnabled(false);
    }
    ui->btnSave->setEnabled(false);

    connect(ui->btnModify, &QPushButton::clicked, this, &FormChannelCoefficient::onModify);
    connect(ui->btnSave, &QPushButton::clicked, this, &FormChannelCoefficient::onSave);
}

void FormChannelCoefficient::init()
{
    // 是否将要修改
    m_willModify = false;
}

void FormChannelCoefficient::onModify()
{
    m_willModify = !m_willModify;
    for (int i = 0; i < CHANNEL_COUNT; i++) {
        QString id = QString("%1").arg(i, 3, 10, QLatin1Char('0'));
        QDoubleSpinBox* dsb = ui->gbxChannelCoefficient->findChild<QDoubleSpinBox*>(QString("dsbD%1").arg(id));
        dsb->setEnabled(m_willModify);
    }
    ui->btnSave->setEnabled(m_willModify);
}

void FormChannelCoefficient::onSave()
{
    // 系数列表
    QList<double> coefficentList;

    m_willModify = false;
    for (int i = 0; i < CHANNEL_COUNT; i++) {
        QString id = QString("%1").arg(i, 3, 10, QLatin1Char('0'));
        QDoubleSpinBox* dsb = ui->gbxChannelCoefficient->findChild<QDoubleSpinBox*>(QString("dsbD%1").arg(id));
        dsb->setEnabled(false);

        coefficentList << dsb->value();
    }
    ui->btnSave->setEnabled(false);

    // 更新系数信号通知
    emit sigUpdateChannelCoefficient(coefficentList);
}
